const request=require('../../utils/request.js');
const globleurl = require('../../utils/serverconfig.js').url;
const formattime=require('../../utils/formattime.js');
const app=getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userinfo:{avatarUrl:globleurl+'/images/logo.png'},
    ordericon1:globleurl+'/images/order.png',
    ordericon2:globleurl+'/images/fav.png',
    ordericon3:globleurl+'/images/conm.png',
    ordericon4:globleurl+'/images/kefu.png',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function() {
    
     let that=this;
      let userinfodes=wx.getStorageSync('userinfodes');
     
           if(!userinfodes){
          //如果缓存不存在
         return
        }else{
          //存在
          let time=600;
           if(Date.now()-userinfodes.time>1000*time){
             //过期
            wx.setStorageSync('userinfodes', '')
           }else{
            //  没有过期
           
            

            this.setData({
              userinfo:userinfodes.userInfo,
              userid:userinfodes.userid
            });
            app.globalData.userInfo=userinfodes.userInfo;
            app.globalData.openid=userinfodes.openid;
       

           }
        }
   
  


     
  },

  //点击跳转我的收藏
  mycollect(){
    let userid=this.data.userid
    if(userid){
      wx.navigateTo({
        url: `./collect/collect?userid=${userid}`
      })
    }
  
  },

  //点击跳转我的评论
  mycomment(){
    let userid=this.data.userid
    if(userid){
      wx.navigateTo({
        url: `./comment/comment?userid=${userid}`
      })
    }
  
  },

  //点击跳转我的订单
  myorder(){
    let userid=this.data.userid
    if(userid){
      wx.navigateTo({
        url: `./myorder/myorder?userid=${userid}`
      })
    }
  
  },


    getOpenId(code){
    let appid='wx69ce6feb6d686a71';
    
    return new Promise(async (resolve,rejsect)=>{
  
      
      let res=await request.myrequest('/miniapp/getOpenId',{appid,code})
      
      resolve(res)
    })
  },


   userLogin(){
    return new Promise((resolve,reject)=>{
      let that=this;
      wx.login({
        success (res) {
          let code=res.code;
          that.getOpenId(code).then(v=>{    
               that.setData({
                  openid:v.data.openid
              });
              resolve(v.data.openid)
          });
        }
      });
    })
  },



  async getuserid(openid){
    let userid=await request.myrequest('/miniapp/requestuserid',{openid},'POST');
    
    this.setData({
      userid:userid.data[0].user_id
    });
  },

  //登录事件
  async bindGetUserInfo (res) {
    let that=this;
    let openid;
    that.userLogin().then(v=>{
      openid=v;
    })

    wx.getUserProfile({
      desc: '请授权获取用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: async (res) => {
     
      this.setData({
      userinfo:res.userInfo
    });


     app.globalData.userInfo=res.userInfo;
     app.globalData.openid=openid;
    

    //存入缓存
    let userinfodes={};
    userinfodes.userInfo=res.userInfo;
    userinfodes.openid=openid;
    userinfodes.userid=this.data.userid;
    userinfodes.time=Date.now();


    wx.setStorageSync('userinfodes', userinfodes);
   

   
   
        wx.showToast({
      title: '登陆成功！',
      icon:'success',
      duration:1000
    });
    setTimeout(()=>{
      wx.switchTab({
        url: '../index/index'
      })
    },1500);
 //插入数据库用户信息
    let last_login=formattime.formatTime(new Date);
    let userInfo=res.userInfo;
    // console.log(userInfo);
    await request.myrequest('/miniapp/applogin',{nickName:userInfo.nickName,gender:userInfo.gender,avatarUrl:userInfo.avatarUrl,last_login:last_login,openid},'POST');
     that.getuserid(openid);
      }
    })
  }

})